﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Domain.IRepository.SiteLibraryManage;
using YunSpace.Repository.SiteLibraryManage;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Data;
using YunSpace.Domain.ViewModel.SiteLibraryManage;
using YunSpace.Domain.ViewModel.SiteMgr.SalesTarget;

#endregion

namespace YunSpace.Application.SiteLibraryManage
{
    public class ActivitiesCaseApp
    {
        private IActivitiesCaseRepository service = new ActivitiesCaseRepository();
        private PhotosApp photosApp = new PhotosApp();

        public List<ActivitiesCaseEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<ActivitiesCaseEntity>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["keyword"].IsEmpty())
            {
                string keyword = queryParam["keyword"].ToString();
                expression = expression.And(t => t.F_ActivitiesTitle.Contains(keyword));
            }
            if (!queryParam["F_SiteId"].IsEmpty())
            {
                string F_SiteId = queryParam["F_SiteId"].ToString();
                expression = expression.And(t => t.F_SiteId == F_SiteId);
            }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public ActivitiesCaseOutput GetForm(string keyValue)
        {
            using (var db = new RepositoryBase())
            {
                ActivitiesCaseOutput data = new ActivitiesCaseOutput();
                string strSql1 = @"select * from Site_ActivitiesCase where F_Id='" + keyValue + "';";
                string strSql2 = @"select * from SL_Photos where photoable_id='" + keyValue + "' and photoable_type='ActivitiesCase' and F_DeleteMark=0;";
                string strSql3 = @"select * from SL_Photos where photoable_id='" + keyValue + "' and photoable_type='ActivitiesCaseVideo' and F_DeleteMark=0;";

                var data1 = db.FindList<ActivitiesCaseEntity>(strSql1);
                var data2 = db.FindList<PhotosEntity>(strSql2);
                var data3 = db.FindList<PhotosEntity>(strSql3);
                data.activitiesCaseEntity = data1.FirstOrDefault();
                data.photosList = data2;
                data.VideoList = data3;
                return data;
            }
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(ActivitiesCaseEntity activitiescaseEntity, List<PhotosEntity> imgList, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                activitiescaseEntity.Modify(keyValue);
                service.Update(activitiescaseEntity);
            }
            else
            {
                activitiescaseEntity.Create();
                service.Insert(activitiescaseEntity);
            }
            if (imgList != null && imgList.Count > 0)
            {
                photosApp.updatePhotoList(imgList, activitiescaseEntity.F_Id);
            }
        }

        /// <summary>
        /// 获取场地台账列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public List<ActivitiesCaseBillViewModel> GetActivitiesCaseBillDtos(Pagination pagination, GetActivitiesCaseBillGridJsonRequest request)
        {
            var dtos = service.GetActivitiesCaseBillDtos(pagination, request);
            var vms = new List<ActivitiesCaseBillViewModel>();

            foreach (var dto in dtos)
            {
                var activitiesDate = (dto.ActivitiesStarDate == null ? "" : dto.ActivitiesStarDate.ToString()) + " ~ " +
                                     (dto.ActivitiesEndDate == null ? "" : dto.ActivitiesEndDate.ToString());

                var vm = new ActivitiesCaseBillViewModel()
                {
                    ActivitiesDate = activitiesDate,
                    ActivitiesTitle = dto.ActivitiesTitle,
                    ActivitiesType = dto.ActivitiesType,
                    AfterTaxProfit = dto.AfterTaxProfit ?? 0,
                    CityName = dto.CityName,
                    Contact = dto.Contact,
                    DbAdminId = dto.DbAdminId,
                    Department = dto.Department,
                    FeeType = dto.FeeType ?? 0,
                    IncomeCash = dto.IncomeCash ?? 0,
                    MochaItom = dto.MochaItom,
                    ProfitMargin = dto.ProfitMargin ?? 0,
                    Seller = dto.Seller,
                    SignedTime = dto.SignedTime?.ToString(),
                    SiteId = dto.SiteId,
                    SiteTitle = dto.SiteTitle,
                    SiteType = dto.SiteType,
                    SpendCash = dto.SpendCash ?? 0,
                    Tax = dto.Tax ?? 0
                };

                vms.Add(vm);
            }

            return vms;
        }
    }
}

